package com.hwpt.controller.admin;


import com.hwpt.dto.admin.AdminDTO;
import com.hwpt.result.Result;
import com.hwpt.service.AdminAuthService;
import com.hwpt.utils.JwtUtil;
import com.hwpt.vo.admin.AdminLoginVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/auth")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "管理员认证接口")
@Controller("adminAuthController")
public class AuthController {

    private final AdminAuthService adminAuthService;

    private final JwtUtil jwtUtil;

    @PostMapping("/login")
    @ApiOperation("管理员登录")
    public Result<AdminLoginVo> login(@RequestBody AdminDTO adminDTO) {
        log.info("管理员登录：{}", adminDTO);
        AdminLoginVo adminLoginVO = adminAuthService.login(adminDTO);

        //登录成功后，生成jwt令牌
        String adminId = adminLoginVO.getAdminId().toString();
        String token = jwtUtil.getToken(adminId);
        adminLoginVO.setToken(token);
        return Result.success(adminLoginVO);
    }



    /**
     * 退出
     *
     * @return
     */
    //退出
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

}
